package com.lx.mobileplayer.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;

/**
 * 类说明：更据资源id获取资源的工具类,使用前在
 * Created by liuxiong on 2019/3/23.
 */

public class ResourcesUtil {

    public static Context mContext;

    /**
    * 方法说明:初始化工具类
    * created by liuxiong on 2019/3/23 16:16
    */
    public static void init(Context context){
        mContext=context.getApplicationContext();
    }

    public static Resources getResources(){
        if(mContext==null){
            throw new IllegalStateException("请先初始化工具类");
        }

        return mContext.getResources();
    }

    public static Drawable getDrawable(int resId)
    {
        return getDrawable(getResources(), resId);
    }

    public static Drawable getDrawable(Resources resources, int resId)
    {
        return getDrawable(resources, resId, null);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressWarnings("deprecation")
    public static Drawable getDrawable(Resources resources, int resId, Resources.Theme theme)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            return resources.getDrawable(resId, theme);
        else
            return resources.getDrawable(resId);
    }

    /**
    * 方法说明:  获取字符串、字符串数组资源
    * created by liuxiong on 2019/4/26 11:07
    */
    public static String getString(int resId) {
        return getResources().getString(resId);
    }
    public static String getString(int resId,Object... objects) {
        return getResources().getString(resId,objects);
    }

    public static String[] getStringArray(int resId) {
        return getResources().getStringArray(resId);
    }
}
